﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Diagnostics;

namespace GpsTagger.Logic
{
    class PictureTagger
    {
        protected string m_pathToExifTool;
        protected bool m_exists;

        protected bool errorFlag;
        protected string errorString;
        protected string runResult;

        public PictureTagger(string pathToExifTool)
        {
            // check if file exists
            m_exists = File.Exists(pathToExifTool);
            if (m_exists)
                m_pathToExifTool = pathToExifTool;
        }

        public bool ValidTagger()
        {
            return m_exists;
        }

        public string GetLastOutput()
        {
            return runResult + Environment.NewLine + errorString;
        }

        public bool RunCommand(string command)
        {
            errorFlag = false;         
            if (m_exists && command.Length > 0)
            {
                ProcessStartInfo psi = new ProcessStartInfo(m_pathToExifTool, command);
                psi.RedirectStandardOutput = true;
                psi.RedirectStandardError = true;
                psi.RedirectStandardInput = true;
                psi.UseShellExecute = false;
                //psi.WindowStyle = ProcessWindowStyle.Minimized;
                psi.CreateNoWindow = true;
                psi.Verb = "Open";
                Process p = Process.Start(psi);                
                p.WaitForExit(600);
                     
                if (!p.HasExited)
                {
                    p.Kill();
                }
                
         
                runResult = p.StandardOutput.ReadToEnd();
                string error = p.StandardError.ReadToEnd();
                if (error.Length > 0)
                {
                    errorFlag = true;
                    errorString = error;
                    errorString += Environment.NewLine + "Command line: " + command;
                }
            }
            else
            {
                errorFlag = true;               
                if (!m_exists)
                    errorString = "ExifTool does not exist";
                if (command.Length == 0)
                    errorString = "No command given to tool";
            }
            return (!errorFlag);
        }

        public bool TagPicture(string filename, MapPoint point, string copyrightInfo)
        {
            bool result = false;            
            StringBuilder tags = new StringBuilder();
            tags.Append("-gps:GPSLatitude=\"");
            tags.Append(Math.Abs(point.LatitudeDegrees));
            tags.Append(",");
            tags.Append(point.LatitudeMinutes);
            tags.Append(",");
            tags.Append(point.LatitudeSeconds);
            tags.Append("\" -gps:GPSLatitudeRef=\"");
            if (point.LatitudeDegrees > 0)
                tags.Append("N\" ");
            else
                tags.Append("S\" ");
            tags.Append("-gps:GPSLongitude=\"");
            tags.Append(Math.Abs(point.LongitudeDegrees));
            tags.Append(",");
            tags.Append(point.LongitudeMinutes);
            tags.Append(",");
            tags.Append(point.LongitudeSeconds);
            tags.Append("\" -gps:GPSLongitudeRef=\"");
            if (point.LongitudeDegrees > 0)
                tags.Append("E\" ");
            else
                tags.Append("W\" ");
            tags.Append("-Copyright=\"");
            tags.Append(copyrightInfo);
            tags.Append("\" ");
            tags.Append(" -overwrite_original ");

            if (!File.Exists(filename))
            {
                runResult = "File To Tag does not exist: " + filename;
                errorString = runResult;
                errorFlag = true;
            }

            tags.Append("\""+filename+"\"");

            result = RunCommand(tags.ToString());

            return result;
        }
    }
}